package com.cw.lang.common.framework.utils;


import com.cw.base.common.model.constants.RestConstant;
import com.cw.base.common.model.constants.RpcConstant;
import com.cw.base.common.model.dto.base.BaseRequest;
import com.cw.base.common.model.dto.base.BaseUserRequest;
import com.cw.lang.common.utils.context.ThreadContextStoreUtil;

/**
 * @author chenw
 * @version $$Id: RpcAppParamUtil.java, v 0.1 2018/12/27 9:23 AM chenw Exp $$
 */
public class RpcAppParamUtil {

    private RpcAppParamUtil() {
    }

    /**
     * 获取RPC传递时的{@link BaseRequest}
     *
     * @return RPC传递时的 {@link BaseRequest}
     */
    public static BaseRequest getRpcBaseRequest() {
        return (BaseRequest) ThreadContextStoreUtil.getInstance()
            .get(RestConstant.RPC_BASE_REQUEST);
    }

    /**
     * 设置RPC传递时的{@link BaseRequest}
     *
     * @param baseRequest RPC传递时的 {@link BaseRequest}
     */
    public static void setRpcBaseRequest(BaseRequest baseRequest) {
        if (baseRequest == null) {
            return;
        }
        ThreadContextStoreUtil.getInstance()
            .set(RestConstant.RPC_BASE_REQUEST, baseRequest);
    }

    /**
     * 清理{@link BaseRequest}
     */
    public static void removeRpcBaseRequest() {
        ThreadContextStoreUtil.getInstance()
            .removeKey(RestConstant.RPC_BASE_REQUEST);
    }

    /**
     * 获取RPC传递时的{@link BaseUserRequest}
     *
     * @return RPC传递时的 {@link BaseUserRequest}
     */
    public static BaseUserRequest getRpcBaseUserRequest() {
        return (BaseUserRequest) ThreadContextStoreUtil.getInstance()
            .get(RestConstant.RPC_BASE_USER_REQUEST);
    }

    /**
     * 设置RPC传递时的{@link BaseUserRequest}
     *
     * @param baseUserRequest RPC传递时的 {@link BaseUserRequest}
     */
    public static void setRpcBaseUserRequest(BaseUserRequest baseUserRequest) {
        if (baseUserRequest == null) {
            return;
        }
        ThreadContextStoreUtil.getInstance()
            .set(RestConstant.RPC_BASE_USER_REQUEST, baseUserRequest);
    }

    /**
     * 清理{@link BaseUserRequest}
     */
    public static void removeRpcBaseUserRequest() {
        ThreadContextStoreUtil.getInstance()
            .removeKey(RestConstant.RPC_BASE_USER_REQUEST);
    }

    /**
     * 获取RPC异常时，是抛异常还是组装为{@link CommonResponse}
     *
     * @return true-抛异常，false-组装{@link CommonResponse}
     */
    public static boolean isRpcExceptionThrowEnabled() {
        return (boolean) ThreadContextStoreUtil.getInstance()
            .get(RpcConstant.RPC_EXCEPTION_THROW_ENABLED);
    }

    /**
     * 设置RPC异常时，是抛异常还是组装为{@link CommonResponse}
     *
     * @param rpcExceptionThrowEnabled true-抛异常，false-组装{@link CommonResponse}
     */
    public static void setRpcExceptionThrowEnabled(boolean rpcExceptionThrowEnabled) {
        ThreadContextStoreUtil.getInstance()
            .set(RpcConstant.RPC_EXCEPTION_THROW_ENABLED, rpcExceptionThrowEnabled);
    }

    /**
     * 清理RPC异常时，是抛异常还是组装为{@link CommonResponse}的配置
     */
    public static void removeRpcExceptionThrowEnabled() {
        ThreadContextStoreUtil.getInstance()
            .removeKey(RpcConstant.RPC_EXCEPTION_THROW_ENABLED);
    }
}
